// import { Store } from 'element-plus/es/components/table/src/store'
import { createStore, Store, useStore as useVuexStore } from 'vuex'

import { getPageList, deletePageDateById } from "@/service/main/system/system"


// 模块
import loginModule from "./login/login"
import systemModule from "./main/system/system"
import dashboardModule from "./main/analysis/dashboard"

// 类型
import type { IRootState } from "./types"
import type { IStroeTyep } from "./types"

const store = createStore<IRootState>({
  state: {
    name: 'string',
    entireDepartments: [],
    entireRoles: [],
    entireMenus: []
  },
  mutations: {
    changeEntireDepartments(state, entireDepartments) {
      state.entireDepartments = entireDepartments
    },
    changeEntireRoles(state, entireRoles) {
      state.entireRoles = entireRoles
    },
    changeEntireMenus(state, entireMenus) {
      state.entireMenus = entireMenus
    },
  },
  actions: {
    async getInitalDataAction({ commit }) {
      const { data: department } = await getPageList('/department/list', { offset: 0, size: 100 })
      const { data: entireRoles } = await getPageList('/role/list', { offset: 0, size: 100 })
      const { data: entireMenus } = await getPageList("menu/list", {})

      commit("changeEntireDepartments", department.list)
      commit("changeEntireRoles", entireRoles.list)
      commit("changeEntireMenus", entireMenus.list)
    }
  },
  modules: {
    loginModule,
    systemModule,
    dashboardModule
  }
})

export default store

// 保持用户刷新的信息
export function setupStore() {
  store.dispatch("loginModule/loadLocalCache")
  store.dispatch("getInitalDataAction")
}

// 设置类型链接,模块的类型
export function useStore(): Store<IStroeTyep> {
  return useVuexStore()
}